#coding:utf-8

from lib.models import Course
from lib.models import Grade
from lib.models import Settings
from google.appengine.ext import db
from lib import students_parser
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.runtime import DeadlineExceededError
from datetime import datetime


def delete_old_grades(cid, period):
    query = Grade.all().filter("cid =", cid).filter("period =", period)
    db.delete(query)


def add_grades(cid, cname, grades_list, period):
    for item in grades_list:
        if period==item['period']:
            newgrade = Grade()
            newgrade.cid = cid
            newgrade.course_name = cname
            newgrade.grade = item['grade']
            newgrade.sid = item['sid']
            newgrade.period = item['period']
            newgrade.put()



def apply_changes(june, febr, sept, course, grades_list):
    periods  = [u"ΙΟΥΝ", u"ΦΕΒΡ", u"ΣΕΠΤ"]
    has_changes= {u"ΙΟΥΝ":june, u"ΦΕΒΡ":febr, u"ΣΕΠΤ":sept}

    for period in periods:
        if has_changes[period]:
            if period==u"ΙΟΥΝ":
                course.has_june = True
            elif period == u"ΦΕΒΡ":
                course.has_febr = True
            elif period == u"ΣΕΠΤ":
                course.has_sept = True
                
            delete_old_grades(course.cid, period)
            add_grades(course.cid, course.name, grades_list, period)


def has_results(course):
    is_done = False
    sett_query = db.GqlQuery("SELECT * FROM Settings")
    settings = sett_query.fetch(1)[0]
    if settings.period == u"ΙΟΥΝ":
        if course.has_june:
            is_done = True
    elif settings.period == u"ΦΕΒΡ":
        if course.has_febr:
            is_done = True

    elif settings.period == u"ΣΕΠΤ":
        if (course.has_june or course.has_febr) and course.has_sept:
            is_done=True

    return is_done

def has_updates(course, soup):
    
    sett_query = db.GqlQuery("SELECT * FROM Settings")
    settings = sett_query.fetch(1)[0]
    it_should = False
    #print settings.period

    if settings.period == u"ΙΟΥΝ":
        it_should = students_parser.has_grades_for(soup, u"ΙΟΥΝ")
        course.has_june = it_should
    elif settings.period == u"ΦΕΒΡ":
        it_should = students_parser.has_grades_for(soup, u"ΦΕΒΡ")
        course.has_febr = it_should

    elif settings.period == u"ΣΕΠΤ":
        has_sept=False
        if course.has_sept == False:
            has_sept = students_parser.has_grades_for(soup, u"ΣΕΠΤ")
            course.has_sept = has_sept


        has_febr=False
        has_june=False
        if (course.has_febr or course.has_june) == False:

            has_febr = students_parser.has_grades_for(soup, u"ΦΕΒΡ")
            course.has_febr = has_febr

            has_june = students_parser.has_grades_for(soup, u"ΙΟΥΝ")
            course.has_june = has_june

        if (has_june or has_febr or has_sept):
            it_should = True


    return it_should




def update(course):
    page = students_parser.get_course_page(course.cid)
    soup = page
    cname = students_parser.get_course_name(soup)
    course.name = cname

    if has_updates(course, soup):
        grades_list= students_parser.get_grades_list(soup)
        apply_changes(course.has_june, course.has_febr, course.has_sept, course, grades_list)

    course.has_results = has_results(course)

    course.updated = course.updated + 1
    course.put()



